<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产地" prop="factoryId">
        <el-select v-model="queryParams.factoryId" placeholder="请选择" filterable clearable @change="factoryChange">
          <el-option v-for="team in factoryList" :key="team.deptId" :label="team.deptName" :value="team.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item label="班组" prop="teamId">
        <el-select v-model="queryParams.teamId" placeholder="请选择" clearable>
          <el-option v-for="team in teamList" :key="team.deptId" :label="team.deptName" :value="team.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item label="合同号" prop="contractCode">
        <el-input
          v-model="queryParams.contractCode"
          placeholder="请输入合同号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="bomName">
        <el-input
          v-model="queryParams.bomName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
         <el-option
           v-for="dict in auditStatusList"
           :key="dict.value"
           :label="dict.label"
           :value="dict.value"
         />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['adjust:adjustapply:edit']"
        >审核</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="adjustapplyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合同号" align="center" prop="contractCode" />
      <el-table-column label="订单编码" align="center" prop="orderCode" />
      <el-table-column label="产品编号" align="center" prop="bomCode" />
      <el-table-column label="产品名称" align="center" prop="bomName" />
      <!--<el-table-column label="规格型号" align="center" prop="productStandard" />-->
      <el-table-column label="产地" align="center" prop="factoryName" />
      <el-table-column label="班组" align="center" prop="teamName" />
      <el-table-column label="调整前开始时间" align="center" prop="nplanStartDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.nplanStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调整前完成时间" align="center" prop="nplanEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.nplanEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调整后开始时间" align="center" prop="oplanStartDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.oplanStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调整后完成时间" align="center" prop="oplanEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.oplanEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调整人" align="center" prop="creater" />
      <el-table-column label="调整时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态
0待审核
1审核成功" align="center" prop="status" />
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['adjust:adjustapply:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['adjust:adjustapply:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改排产调整申报审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编码" prop="orderCode">
          <el-input v-model="form.orderCode" placeholder="请输入订单编码" />
        </el-form-item>
        <el-form-item label="订单产品ID sm_order_product" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入订单产品ID sm_order_product" />
        </el-form-item>
        <el-form-item label="产品编号--产品的半成品" prop="bomCode">
          <el-input v-model="form.bomCode" placeholder="请输入产品编号--产品的半成品" />
        </el-form-item>
        <el-form-item label="产品名称--产品的半成品" prop="bomName">
          <el-input v-model="form.bomName" placeholder="请输入产品名称--产品的半成品" />
        </el-form-item>
        <el-form-item label="规格型号" prop="productStandard">
          <el-input v-model="form.productStandard" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="制造公司名称" prop="factoryName">
          <el-input v-model="form.factoryName" placeholder="请输入制造公司名称" />
        </el-form-item>
        <el-form-item label="制造公司ID" prop="factoryId">
          <el-input v-model="form.factoryId" placeholder="请输入制造公司ID" />
        </el-form-item>
        <el-form-item label="制造班组名称" prop="teamName">
          <el-input v-model="form.teamName" placeholder="请输入制造班组名称" />
        </el-form-item>
        <el-form-item label="制造班组ID" prop="teamId">
          <el-input v-model="form.teamId" placeholder="请输入制造班组ID" />
        </el-form-item>
        <el-form-item label="工单ID" prop="workorderId">
          <el-input v-model="form.workorderId" placeholder="请输入工单ID" />
        </el-form-item>
        <el-form-item label="合同号" prop="contractCode">
          <el-input v-model="form.contractCode" placeholder="请输入合同号" />
        </el-form-item>
        <el-form-item label="调整前排产计算开始时间" prop="nplanStartDate">
          <el-date-picker clearable
            v-model="form.nplanStartDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择调整前排产计算开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="调整前排产预计完成时间" prop="nplanEndDate">
          <el-date-picker clearable
            v-model="form.nplanEndDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择调整前排产预计完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="调整后排产计算开始时间" prop="oplanStartDate">
          <el-date-picker clearable
            v-model="form.oplanStartDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择调整后排产计算开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="调整后排产预计完成时间" prop="oplanEndDate">
          <el-date-picker clearable
            v-model="form.oplanEndDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择调整后排产预计完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="调整人" prop="creater">
          <el-input v-model="form.creater" placeholder="请输入调整人" />
        </el-form-item>
        <el-form-item label="调整时间" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择调整时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核时间" prop="auditTime">
          <el-date-picker clearable
            v-model="form.auditTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAdjustapply, getAdjustapply, delAdjustapply, addAdjustapply, updateAdjustapply } from "@/api/sm/order/adjustapply";
import { listTeam,listFactory } from "@/api/system/dept";
export default {
  name: "Adjustapply",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排产调整申报审核表格数据
      adjustapplyList: [],
      factoryList: [],
      teamList: [],
      auditStatusList: [
        {
          "label": "待审核",
          "value": 0
        },
        {
          "label": "审核完成",
          "value": 1
        }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        bomCode: null,
        bomName: null,
        factoryId: null,
        teamId: null,
        workorderId: null,
        contractCode: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderCode: [
          { required: true, message: "订单编码不能为空", trigger: "blur" }
        ],
        productId: [
          { required: true, message: "订单产品ID sm_order_product不能为空", trigger: "blur" }
        ],
        bomCode: [
          { required: true, message: "产品编号--产品的半成品不能为空", trigger: "blur" }
        ],
        bomName: [
          { required: true, message: "产品名称--产品的半成品不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getFactoryList();
  },
  methods: {
    getFactoryList() {
      this.loading = true;
      listFactory({"level":2}).then(response => {
        this.factoryList = response.data;

        this.queryParams.factoryId = this.factoryList[0].deptId;
        this.getTeamList(this.queryParams.factoryId);
        //this.teamList = [];

        this.loading = false;
      });
    },
    factoryChange(){
      this.getTeamList(this.queryParams.factoryId);
    },
    /** 查询订单信息列表 */
    getTeamList(parentId) {
      this.loading = true;
      listTeam({"parentId":parentId}).then(response => {
        this.teamList = response.data || [];
        this.loading = false;
      });
    },
    /** 查询排产调整申报审核列表 */
    getList() {
      this.loading = true;
      listAdjustapply(this.queryParams).then(response => {
        this.adjustapplyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderCode: null,
        productId: null,
        bomCode: null,
        bomName: null,
        productStandard: null,
        factoryName: null,
        factoryId: null,
        teamName: null,
        teamId: null,
        workorderId: null,
        contractCode: null,
        nplanStartDate: null,
        nplanEndDate: null,
        oplanStartDate: null,
        oplanEndDate: null,
        creater: null,
        createDate: null,
        status: null,
        auditTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加排产调整申报审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAdjustapply(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改排产调整申报审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAdjustapply(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAdjustapply(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除排产调整申报审核编号为"' + ids + '"的数据项？').then(function() {
        return delAdjustapply(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('adjust/adjustapply/export', {
        ...this.queryParams
      }, `adjustapply_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
